package cn.edu.njust.control;

import cn.edu.njust.pojo.User;
import cn.edu.njust.service.LoginService;
import cn.edu.njust.service.impl.LoginServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "loginServlet", value = "/loginServlet")
public class LoginServlet extends HttpServlet {
    public LoginService ls = new LoginServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/json;charset=UTF-8");

        String id = request.getParameter("id_no");
        String password = request.getParameter("password");
        String checkCode = request.getParameter("checkCode");

        User loginUser = new User();
        loginUser.setIdNo(id);
        loginUser.setPassword(password);

        // 对比验证码信息
        HttpSession session = request.getSession();
        String checkCodeGen = (String) session.getAttribute("checkCodeGen");

        // 如果用户生成的验证码错误,给出提示信息
        if (!checkCodeGen.equalsIgnoreCase(checkCode)) {
            // request.setAttribute("login_msg","loginSuccess");
            request.setAttribute("login_msg","验证码错误！");
            request.getRequestDispatcher("index.jsp").forward(request,response);
            return;
        }

        User user = ls.loginResult(loginUser);
        if (user == null) {
            // 用户不存在，给出提示信息
            request.setAttribute("login_msg","用户名或密码错误！");
            request.getRequestDispatcher("index.jsp").forward(request,response);
        } else {
            // 保存当前登录用户信息
            session.setAttribute("loginUser", user);
            // 返回登录成功信息
            response.getWriter().write("loginSuccess");
            // 查询身份
            int identity = ls.queryIdentity(user);
            //查询是否预报名
            int status = ls.queryStatus(user);
            // 获取去虚拟目录
            String contextPath = request.getContextPath();
            if (identity == 0) {
                // 学生,然后再进行判断该学生是否预报名
                    if(status == 1){
                        //已经预报名过了，直接跳转到与报名信息展示页面
                        response.sendRedirect("applyInfo.jsp");
                    }else{
                        //还没预报名，直接跳转到填报预报名信息页面
                        response.sendRedirect("preRegister.jsp");
                    }
            } else if (identity == 1) {
                // 教师
                response.sendRedirect("teacherInfo.jsp");
            } else {
                // 系统管理员
                response.sendRedirect("resetPassword.jsp");
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        this.doGet(request, response);
    }
}
